// pages/myPage/pages/user_edit/edit.js
const util = require('../../../../utils/util.js')
const config = require('../../../../config')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: [],
    avatarUrl:''
  },

  chooseImage() {
    var that = this
    wx.chooseImage({
      success: function (res) {
        that.setData({
          avatarUrl: res.tempFilePaths
        })
        var str = 'user.avatar'
        wx.uploadFile({
          url: config.uploadAvatar,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'userId': app.globalData.userId
          },
          success: function (res) {
            that.setData({
              [str]: res.data
            })
          }, fail: function (err) {
            console.log(err)
          }
        })
      }
    })
  },

  userNameInput:function(e){
    var str = 'user.userName';
    this.setData({
      [str]:e.detail.value
    })
  },

  radioChange:function(e){
    var str = 'user.gender';
    this.setData({
      [str]: e.detail.value
    })
  },

  studentIdInput:function(e){
    var str = 'user.studentId';
    this.setData({
      [str]: e.detail.value
    })
  },

  passwordInput: function (e) {
    var str = 'user.password';
    this.setData({
      [str]: e.detail.value
    })
  },

  departmentInput:function(e){
    var str = 'user.department';
    this.setData({
      [str]: e.detail.value
    })
  },

  majorInput:function(e){
    var str = 'user.major';
    this.setData({
      [str]: e.detail.value
    })
  },

  phoneNumInput:function(e){
    var str = 'user.phoneNum';
    this.setData({
      [str]: e.detail.value
    })
  },

  sub_btn:function(){
    var that = this
    if (this.data.user.userName.length == 0) {
      wx.showModal({
        title: '提示',
        content: '姓名不能为空',
      })
    } else if (this.data.user.studentId.length == 0) {
      wx.showModal({
        title: '提示',
        content: '学号不能为空',
      })
    } else if (this.data.user.studentId.length != 13) {
      wx.showModal({
        title: '提示',
        content: '学号格式错误',
      })
    } else if (this.data.user.password.length == 0) {
      wx.showModal({
        title: '提示',
        content: '密码不能为空',
      })
    } else if (this.data.user.department.length == 0) {
      wx.showModal({
        title: '提示',
        content: '院系不能为空',
      })
    } else if (this.data.user.major.length == 0) {
      wx.showModal({
        title: '提示',
        content: '专业不能为空',
      })
    } else if (this.data.user.phoneNum.length == 0) {
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
      })
    } else if (this.data.user.phoneNum  .length != 11) {
      wx.showModal({
        title: '提示',
        content: '手机号格式错误',
      })
    }else{
      util.editUserInfo(that)
      wx.navigateBack({
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.getUser(this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})
var sex = [{
  key: "1",
  value: "男"
}, {
  key: "2",
  value: "女"
}];
